﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace VietPublisher.BaseProject.Utils.SiteMaps
{
    class SiteMapImpl : SiteMap
    {

        private Site[] sites = null;

        public SiteMapImpl()
        {
            this.sites = new SiteImpl[] { };
        }

        public SiteMapImpl(Site site)
        {
            this.sites = new SiteImpl[] { };
            addSite(site);
        }

        public SiteMapImpl(Site[] sites)
        {
            this.sites = sites;
        }

        public List<Site> getSites()
        {
            return new List<Site>(sites);
        }

        /**
         * add Sitemap object to sites list
         */
        public void addSites(SiteMap sitemap)
        {
            List<Site> tempSite = new List<Site>(sites.ToList());
            List<Site> iterator = sitemap.getSites();

            foreach (var site in iterator)
            {
                if (!tempSite.Contains(site))
                {
                    tempSite.Add(site);
                }
            }

            sites = tempSite.ToArray();
        }

        /**
         * add Site object to sites list
         */
        public void addSite(Site site)
        {
            List<Site> tempSite = sites.ToList<Site>();

            if (tempSite.Count > 0)
            {
                Boolean checkAlike = false;
                foreach (Site item in tempSite)
                {
                    Site siteValue = item;
                    if (siteValue.getLabel().Equals(site.getLabel()))
                    {
                        checkAlike = true;
                        break;
                    }
                }
                if (!checkAlike)
                    tempSite.Add(site);

            }
            else tempSite.Add(site);
            sites = tempSite.ToArray();

        }


        public void updateSite(String urlSite)
        {

            List<Site> tempSite = this.sites.ToList();
            int check = -1;
            int i = 0;
            foreach (var site in tempSite)
            {
                Site siteValue = site;
                if (siteValue.getUrl().Equals("HomePage.portal?" + urlSite))
                {
                    check = i;
                    break;
                }
                i++;
            }

            if (check >= 0)
            {
                for (int index = check + 1; i < tempSite.Count; i++)
                {
                    tempSite.RemoveAt(index);

                }
            }

            sites = tempSite.ToArray();
        }


        public Boolean contain(String urlSite)
        {

            List<Site> tempSite = this.sites.ToList();
            //for (int i = 0; i < tempSite.size(); i++) {
            foreach (var item in tempSite)
            {
                Site siteValue = item;
                if (siteValue.getUrl().Equals("HomePage.portal?" + urlSite))
                {
                    return true;
                }
            }
            return false;

        }



    }//close class
}
